public class Solution2 {
    public static void main(String[] args) {
        TreeNode root = new TreeNode(0);
        root.left = new TreeNode(1);
        System.out.println(sumNumbers(root));
    }
    public static int sumNumbers(TreeNode root) {
        return dfs(root, 0);
    }

    public static int dfs(TreeNode root, int preSum) {
        int sum= preSum * 10 + root.val;
        if(root == null || ( root.left == null && root.right == null)) {
            return sum;
        }
        int leftSum = 0;
        if(root.left != null) {
            leftSum= dfs(root.left, sum);
        }

        int rightSum = 0;
        if(root.right != null) {
            rightSum = dfs(root.right, sum);
        }
        return leftSum + rightSum;
    }
}